package com.ccy.product.controller;

import com.ccy.common.enums.AttrEnum;
import com.ccy.common.utils.PageUtils;
import com.ccy.common.utils.R;
import com.ccy.product.entity.AttrEntity;
import com.ccy.product.entity.ProductAttrValueEntity;
import com.ccy.product.service.AttrService;
import com.ccy.product.service.ProductAttrValueService;
import com.ccy.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author ccy
 * @email ccy@gmail.com
 * @date 2023-06-19 21:42:21
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * 获取分类销售属性
     */
    @RequestMapping("/sale/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R querySaleAttrPage(@RequestParam Map<String, Object> params, @PathVariable Long catelogId){
        PageUtils page = attrService.queryAttrPage(params, catelogId, AttrEnum.ATTR_TYPE_SALE.getCode());
        return R.ok().put("page", page);
    }

    /**
     * 获取spu规格参数
     */
    @GetMapping("/base/listforspu/{spuId}")
    //@RequiresPermissions("product:attr:list")dd
    public R listForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> data = attrService.listForSpu(spuId);
        return R.ok().put("data", data);
    }

    /**
     * 获取分类规格参数
     */
    @RequestMapping("/base/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R queryBaseAttrPage(@RequestParam Map<String, Object> params, @PathVariable Long catelogId){
        PageUtils page = attrService.queryAttrPage(params, catelogId, AttrEnum.ATTR_TYPE_BASE.getCode());
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);

        return R.ok();
    }

    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
